﻿using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Resources;
using System.Xml;

namespace GlobalizationTools.Model
{
    public class Resource
    {
        Project _project;
        readonly string _filePath;
        string _include;
        bool _isLoaded = false;
        bool _isRootResource;
        XmlNode _xmlNode;
        CultureInfo _cultureInfo = null;
        Dictionary<int, Dictionary<string, ResXDataNode>> _data;
        Dictionary<string, object> _metaData;

        public Resource(XmlNode xmlNode, Project project)
        {
            _xmlNode = xmlNode;
            _project = project;
            _include = _xmlNode.Attributes["Include"].Value;
            _filePath = Path.Combine(Path.GetDirectoryName(project.FilePath), _include);
            _isRootResource = _xmlNode.ChildNodes.Count > 0;
            if (!_isRootResource)
            {
                string name = Path.GetFileNameWithoutExtension(_include);
                name = Path.GetExtension(name).Remove(0, 1);
                _cultureInfo = CultureInfo.GetCultureInfo(name);
            }
        }

        public Resource(string path, bool isRoot)
        {
            _filePath = path;
            _isRootResource = isRoot;
            if (!_isRootResource)
            {
                string name = Path.GetFileNameWithoutExtension(_filePath);
                name = Path.GetExtension(name).Remove(0, 1);
                _cultureInfo = CultureInfo.GetCultureInfo(name);
            }
        }

        public void LoadResource()
        {
            ResXResourceReader reader = new ResXResourceReader(_filePath, ResourceUtils.TypeResolutionService);
            reader.BasePath = Path.GetDirectoryName(_filePath);
            reader.UseResXDataNodes = true;
            IDictionaryEnumerator enumerator = reader.GetMetadataEnumerator();
            _metaData = new Dictionary<string, object>();
            while (enumerator.MoveNext())
            {
                _metaData.Add(enumerator.Key.ToString(), enumerator.Value);
            }
            enumerator = reader.GetEnumerator();
            _data = new Dictionary<int, Dictionary<string, ResXDataNode>>();
            while (enumerator.MoveNext())
            {
                ResXDataNode node = (ResXDataNode)enumerator.Value;
                int typeIndex = ResourceUtils.TypesList.IndexOf(node.GetValueTypeName(ResourceUtils.TypeResolutionService));
                if (!_data.ContainsKey(typeIndex))
                {
                    _data.Add(typeIndex, new Dictionary<string, ResXDataNode>());
                }
                _data[typeIndex].Add((string)enumerator.Key, node);
            }
            reader.Close();
            _isLoaded = true;
        }

        public void SaveResource()
        {
            ResXResourceWriter writer = new ResXResourceWriter(_filePath);
            writer.BasePath = Path.GetDirectoryName(_filePath);
            foreach (KeyValuePair<string, object> entry in _metaData)
            {
                writer.AddResource(entry.Key, entry.Value);
            }
            foreach (KeyValuePair<int, Dictionary<string, ResXDataNode>> pair in _data)
            {
                foreach (ResXDataNode data in pair.Value.Values)
                {
                    writer.AddResource(data);
                }
            }
            writer.Generate();
            writer.Close();
        }

        public bool IsLoaded
        {
            get { return _isLoaded; }
        }

        public string Include
        {
            get { return _include; }
        }

        public bool IsRootResource
        {
            get { return _isRootResource; }
        }

        public CultureInfo CultureInfo
        {
            get { return _cultureInfo; }
        }

        public Dictionary<int, Dictionary<string, ResXDataNode>> Data
        {
            get { return _data; }
        }

        public Dictionary<string, object> MetaData
        {
            get { return _metaData; }
        }

        public string FilePath
        {
            get { return _filePath; }
        }

    }
}
